package com.jili.scy.modules.handler;

import com.jili.scy.modules.entity.Scy;
import com.jili.scy.modules.mapper.ScyMapper;
import com.jili.scy.modules.mapper.StatementMapper;
import com.orisdom.hj212.format.hbt212.core.T212Mapper;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author: Jili
 * @date: Created on 2022/4/8 16:51
 */
@Slf4j
public class ScyServerTest extends ChannelInboundHandlerAdapter {

    ScyMapper scyMapper;
    T212Mapper t212Mapper;
    StatementMapper statementMapper;

    public ScyServerTest(ScyMapper scyMapper, StatementMapper statementMapper) {
        this.scyMapper = scyMapper;
        this.t212Mapper = new T212Mapper().enableDefaultParserFeatures();
        this.statementMapper = statementMapper;
    }

    /**
     * 客户端连接会触发
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
//        log.info("Channel active......");
    }

    /**
     * 客户端发消息会触发
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        //String h212 = "##0136ST=32;CN=2011;PW=123456;MN=LD130133000015;CP=&&DataTime=20160824003817;B01-Rtd=36.91;011-Rtd=231.0,011-Flag=N;060-Rtd=1.803,060-Flag=N&&4980\r\n";

        ByteBuf in = (ByteBuf) msg;
        String readStr = in.toString(CharsetUtil.UTF_8);
        readStr = readStr.replaceAll("\n", "");
        log.info("服务器收到数采仪消息: {}", readStr);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (readStr.startsWith("##") && readStr.contains("&&")) {
            try {
                Map<String, Object> data = t212Mapper.readDeepMap(readStr + "\r\n");
                System.out.println(data);
                String deviceSn = (String) data.get("MN");
                Map<String, String> cp = (Map<String, String>) data.get("CP");
                if (!cp.isEmpty()) {
                    Scy scy = new Scy();
                    scy.setRawContent(readStr);
                    scy.setIsViewed(0);
                    scy.setDateAdd(DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
                    scy.setDeviceSn(deviceSn);
                    if ("2011".equals(data.get("CN"))) {
                        scy.setYieldTotal(cp.get("w00001-Rtd") == null ? cp.get("B11-Rtd") : cp.get("w00001-Rtd"));//m^3
                        scy.setYieldInstant(cp.get("w00000-Rtd") == null ? cp.get("B01-Rtd") : cp.get("w00000-Rtd"));//m^3
                        scy.setPh(cp.get("w01001-Rtd") == null ? cp.get("001-Rtd") : cp.get("w01001-Rtd"));//PH
                        scy.setCodcr(cp.get("w01018-Rtd") == null ? cp.get("011-Rtd") : cp.get("w01018-Rtd"));//mg/L
                        scy.setTp(cp.get("w21011-Rtd") == null ? cp.get("101-Rtd") : cp.get("w21011-Rtd"));
                        scy.setTn(cp.get("w21001-Rtd") == null ? cp.get("065-Rtd") : cp.get("w21001-Rtd"));
                        scy.setNh3n(cp.get("w21003-Rtd") == null ? cp.get("060-Rtd") : cp.get("w21003-Rtd"));
                        scy.setSs(cp.get("w02012-Rtd") == null ? cp.get("003-Rtd") : cp.get("w02012-Rtd"));
                        scyMapper.insert(scy);
                    }
                }
            } catch (Exception e) {
                log.warn("解析数采仪消息异常:{}", e.getMessage());
            }
        }
    }


    /**
     * 发生异常触发
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
